from gm.api import *
import datetime
import numpy as np
import pandas as pd
from MSCI_tools import 根据市值对股票池因子中性化的写法 as factor
from MSCI_tools import 计算贝塔值和自定义的波动率_ as beta_and_vol
from MSCI_tools import msci_tools as tools
from MSCI_tools import 计算RSRS as rsrs_fun

def 防守型策略_1(symbol_list,now):
    """
    排名条件设市盈率从小到大，权重2；  PETTM
    股息率从大到小，权重1； DY
    市净率从小到大，权重3； PB
    历史贝塔从小到大，权重2；
    自定义波动率指标从小到大权重10，
    """
    PETTM = factor.fast_batch_get_neutralized_factor(symbol_list,"PETTM",now,more_is_better=False)
    DY = factor.fast_batch_get_neutralized_factor(symbol_list, "DY", now, more_is_better=True)
    PB = factor.fast_batch_get_neutralized_factor(symbol_list, "PB", now, more_is_better=False)

    beta = {}
    volatility = {}


    for symbol in symbol_list:
        beta[symbol] = beta_and_vol.get_beta_weight_2(symbol, now, count=30)
        volatility[symbol] = beta_and_vol.get_volatility_normal(symbol, now, count=30)

    df = pd.DataFrame([])
    df["symbol"] = PETTM.keys()
    df["PETTM"] = PETTM.values()
    df["PB"] = PB.values()
    df["DY"] = DY.values()
    df["beta"] = beta.values()
    df["volatility"] = volatility.values()

    weight = [1,-1,-1,-1,-1]

    symbol_list = tools.from_df_get_symbol_list_by_score(df, weight, start=1)

    return symbol_list